﻿#include "stdafx.h"
#include "DimensionMap.h"
//#include "ui_NetworkParamWidget.h"
#include "QCustomPlot/QTrendChart.h"
#include <QtWidgets/QGridLayout>
#include "TrendChartParam.h"
#include "forms/TrendChartSetting.h"
#include "QEventLoop"
#include "QMouseEvent"
#include "SignalCenter.h"
#include "DataCenter.h"

//曲线 类
class DimensionMap::Internal
{
public:
	Internal(DimensionMap* ptr)
	{
		q = ptr;
		QStringList trendChartNames;
		trendChartNames << tr("阴极涂层宽度")
			<< tr("阳极涂层宽度")
			<< tr("AH宽度")
			<< tr("CH宽度")
			<< tr("上隔膜宽度")
			<< tr("下隔膜宽度")
			<< tr("AnodeUpsepaGapAverage")
			<< tr("CathodeDownsepaGapAverage")
			<< tr("SepaWideAverage")
			<< tr("SealTapeWideAverage");
		for (size_t i = 0; i < 10; i++)
		{
			TrendChartParam param;
			trendChartParamVec.push_back(param);
			float minValue = 63;//<! y方向坐标轴的最小值
			float maxValue = 66;//<! y方向坐标轴的最小值
			QTrendChart* chart1 = new QTrendChart(minValue, maxValue, q);
			chart1->setObjectName(trendChartNames[i]);
			trendChartPtrVec.push_back(chart1);
			connect(chart1, SIGNAL(mousePress(QMouseEvent*)), q, SLOT(slot_TrendChartSetting(QMouseEvent*)),Qt::ConnectionType::UniqueConnection);
			chart1->xAxis->setLabelColor(QColor(255, 255, 255));
			chart1->yAxis->setLabelColor(QColor(255, 255, 255));
			chart1->xAxis->setTickLabelColor(QColor(255, 255, 255));
			chart1->yAxis->setTickLabelColor(QColor(255, 255, 255));
			chart1->xAxis->setTickPen(QColor(255, 200, 200));
			chart1->yAxis->setTickPen(QColor(255, 200, 200));
			chart1->xAxis->setSubTickPen(Qt::NoPen);
			chart1->yAxis->setSubTickPen(Qt::NoPen);
			chart1->xAxis->setBasePen(QColor(255, 200, 200));
			chart1->yAxis->setBasePen(QColor(255, 200, 200));
			chart1->xAxis->grid()->setVisible(false);
			chart1->yAxis->grid()->setVisible(false);


			chart1->xAxis2->setVisible(true);
			chart1->xAxis2->setTickLabels(false);					//设置y轴刻度值不显示
			chart1->xAxis2->setBasePen(QColor(255, 200, 200));		//设置y2轴的绘制画笔
			chart1->xAxis2->setTickPen(Qt::NoPen);					//设置y2轴的主刻度线绘制画笔
			chart1->xAxis2->setSubTickPen(Qt::NoPen);				//设置y2轴的子刻度线绘制画笔
			chart1->yAxis2->setVisible(true);
			chart1->yAxis2->setTickLabels(false);					//设置y轴刻度值不显示
			chart1->yAxis2->setBasePen(QColor(255, 200, 200));		//设置y2轴的绘制画笔
			chart1->yAxis2->setTickPen(Qt::NoPen);					//设置y2轴的主刻度线绘制画笔
			chart1->yAxis2->setSubTickPen(Qt::NoPen);				//设置y2轴的子刻度线绘制画笔

			//<! 设置标题
			QCPLayoutGrid* layout = chart1->plotLayout();
			layout->insertRow(0);
			QCPTextElement* title = new QCPTextElement(chart1, trendChartNames[i]);
			title->setFont(QFont("sans", 12, QFont::Bold));
			title->setTextColor(QColor("#b1965b"));
			title->setMargins(QMargins(0,20,0,0));
			layout->addElement(0, 0, title);

		}
	}
	void initTrendChart(QTrendChart* trendChart, const TrendChartParam& param)
	{
		trendChart->setBackground(QColor(param.bgColor));
		trendChart->setWaveLine(param.name, QColor(param.waveLineColor));
		trendChart->setReferenceLine(param.name, 1, QColor(param.LSL_Color), param.LSL_Value);
		trendChart->setReferenceLine(param.name, 2, QColor(param.USL_Color), param.USL_Value);
		trendChart->setReferenceLine(param.name, 3, QColor(param.StandardColor), param.StandardValue);

		trendChart->yAxis->setRange(param.yAxisMin, param.yAxisMax);
		trendChart->xAxis->setRange(0, 20);
		//trendChart->yAxis->setLabel("FoilWidth");
		//trendChart->yAxis->setLabelColor(Qt::white);
	}
	void initContrl()
	{
		q->loadCfg();
		for (size_t i = 0; i < trendChartParamVec.size(); i++)
		{
			initTrendChart(trendChartPtrVec[i], trendChartParamVec[i]);
			chartY.push_back(0);
		}
	
		QGridLayout*  gridLayout = new QGridLayout(q->getMainWnd());
		gridLayout->setObjectName(QStringLiteral("gridLayout"));
		gridLayout->setContentsMargins(0, 0, 0, 0);
		
		gridLayout->addWidget(trendChartPtrVec[0], 0, 0, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[1], 0, 1, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[2], 1, 0, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[3], 1, 1, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[4], 2, 0, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[5], 2, 1, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[6], 3, 0, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[7], 3, 1, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[8], 4, 0, 1, 1);
		gridLayout->addWidget(trendChartPtrVec[9], 4, 1, 1, 1);


	}
	void initForm()
	{
		q->setTitle(tr("DIMENSION MAP"));
	}

public:
	DimensionMap* q;

	QVector<TrendChartParam> trendChartParamVec;
	QVector<QTrendChart*> trendChartPtrVec;
	QVector<double> chartY;
};

DimensionMap::DimensionMap(QWidget* parent) :
	CAbstractCard(parent),
	p(new Internal(this))
{
	//p->ui.setupUi(this);
	p->initContrl();
	p->initForm();
	connect(&SignalCenter::GetInstance(), SIGNAL(addCurveData(int, double)),
		this, SLOT(AddData(int, double)));

}

DimensionMap::~DimensionMap()
{
	saveCfg();
	delete p;
	p = nullptr;
	
}

void DimensionMap::loadCfg()
{
	QSettings settings("./Cfg.ini", QSettings::IniFormat);
	for (size_t i = 0; i < p->trendChartParamVec.size(); i++)
	{
		TrendChartParam param;
		param.name = settings.value(QString("TrendChart_%1/Name").arg(i), "Curve").toString();
		param.bgColor = settings.value(QString("TrendChart_%1/BackGroundColor").arg(i), "#2e323a").toString();
		param.waveLineColor = settings.value(QString("TrendChart_%1/WaveLineColor").arg(i), "0eff36").toString();
		param.LSL_Color = settings.value(QString("TrendChart_%1/LSL_Color").arg(i), "#ff0511").toString();
		param.USL_Color = settings.value(QString("TrendChart_%1/USL_Color").arg(i), "#ff0511").toString();
		param.StandardColor = settings.value(QString("TrendChart_%1/StandardColor").arg(i), "#6bc7a4").toString();
		param.LSL_Value = settings.value(QString("TrendChart_%1/LSL_Value").arg(i), 5).toDouble();
		param.USL_Value = settings.value(QString("TrendChart_%1/USL_Value").arg(i), 6).toDouble();
		param.StandardValue = settings.value(QString("TrendChart_%1/StandardValue").arg(i), 4).toDouble();
		param.yAxisMin = settings.value(QString("TrendChart_%1/yAxisMin").arg(i), 0).toDouble();
		param.yAxisMax = settings.value(QString("TrendChart_%1/yAxisMax").arg(i), 8).toDouble();
		p->trendChartParamVec[i] = param;
	}
}

void DimensionMap::saveCfg()
{
	QSettings settings("./Cfg.ini", QSettings::IniFormat);
	for (size_t i = 0; i < p->trendChartParamVec.size(); i++)
	{
		TrendChartParam param = p->trendChartParamVec[i];
		
		settings.setValue(QString("TrendChart_%1/Name").arg(i), param.name);
		settings.setValue(QString("TrendChart_%1/BackGroundColor").arg(i), param.bgColor);
		settings.setValue(QString("TrendChart_%1/WaveLineColor").arg(i), param.waveLineColor);
		settings.setValue(QString("TrendChart_%1/LSL_Color").arg(i), param.LSL_Color);
		settings.setValue(QString("TrendChart_%1/USL_Color").arg(i), param.USL_Color);
		settings.setValue(QString("TrendChart_%1/StandardColor").arg(i), param.StandardColor);
		settings.setValue(QString("TrendChart_%1/LSL_Value").arg(i), param.LSL_Value);
		settings.setValue(QString("TrendChart_%1/USL_Value").arg(i), param.USL_Value);
		settings.setValue(QString("TrendChart_%1/StandardValue").arg(i), param.StandardValue);
		settings.setValue(QString("TrendChart_%1/yAxisMin").arg(i), param.yAxisMin);
		settings.setValue(QString("TrendChart_%1/yAxisMax").arg(i), param.yAxisMax);
		
	}
}

void DimensionMap::AddData(int index, double Value)
{
	if (index < p->trendChartPtrVec.length())
	{ 
		QString name = p->trendChartParamVec[index].name;
		
		p->trendChartPtrVec[index]->plotData(name, p->chartY[index],Value);
		p->chartY[index]++;
	}
}

void DimensionMap::slot_TrendChartSetting(QMouseEvent* event)
{
	if (event->button() == Qt::RightButton)
	{
		QString objName = sender()->objectName();
		QTrendChart* trendChartPtr = nullptr;
		int index = 0;
		for (size_t i = 0; i < p->trendChartPtrVec.size(); i++)
		{
			if (objName == p->trendChartPtrVec[i]->objectName())
			{
				trendChartPtr = p->trendChartPtrVec[i];
				index = i;
				break;
			}
		}
		if (trendChartPtr != nullptr)
		{
			TrendChartSetting setting;
			TrendChartParam param = p->trendChartParamVec[index];
			setting.inputParam(&param, trendChartPtr);
			setting.exec();
			p->trendChartParamVec[index] = param;
		}
	}
}

bool DimensionMap::event(QEvent* event)
{
	return QWidget::event(event);
}